package com.it.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.it.eduservice.entity.EduChapter;
import com.it.eduservice.entity.EduVideo;
import com.it.eduservice.entity.chapter.ChapterVo;
import com.it.eduservice.entity.chapter.VideoVo;
import com.it.eduservice.mapper.EduChapterMapper;
import com.it.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.eduservice.service.EduVideoService;
import com.it.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author test.java
 * @since 2021-03-02
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    //把video注入
    @Autowired
    private EduVideoService eduVideoService;

    //课程章节和小结 根据课程id查询
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1 查询章节
        QueryWrapper<EduChapter>wrapperChapter=new QueryWrapper<EduChapter>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //2 查询小结
        QueryWrapper<EduVideo>wrapperVideo=new QueryWrapper<EduVideo>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(wrapperVideo);

        //封装章节小结
        List<ChapterVo>finalChapterList=new ArrayList<ChapterVo>();
        //遍历章节
        for (int i = 0; i < eduChapterList.size(); i++) {
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalChapterList.add(chapterVo);
            //遍历小结
            List<VideoVo>finalVideoList=new ArrayList<VideoVo>();
            for (int j = 0; j < eduVideoList.size(); j++) {
                EduVideo eduVideo = eduVideoList.get(j);
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    finalVideoList.add(videoVo);
                }
            }
            //把小结放到章节中去
            chapterVo.setChildren(finalVideoList);
        }
        return finalChapterList;
    }

    //删除章节
    public boolean deleteChapter(String chapterId) {
        //第一种删除  在小结表中根据章节id查小结 章节下面有小结 就不删除
        QueryWrapper<EduVideo>wrapper=new QueryWrapper<EduVideo>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        //如果章节下存在小结 不删除
        if(count>0){
            throw new GuliException(20001,"不能删除");
        }else{//没有 删除
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }

    //根据课程id 删除所有章节
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter>wrapper =new QueryWrapper<EduChapter>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);

    }
}
